//
//  MovieView.m
//  随拍
//
//  Created by lanou3g on 16/1/19.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "MovieView.h"
#import <AVFoundation/AVFoundation.h>

@interface MovieView ()

@property (nonatomic ,strong) AVPlayer *player;
@property (nonatomic ,strong) NSString *moviePat;

@end

@implementation MovieView


- (void)addMoviePath:(NSString *)moviePath{
    self.moviePat = moviePath;
    NSString *cachepath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *savePath = [cachepath stringByAppendingPathComponent:moviePath];
    NSURL *saveUrl = [NSURL fileURLWithPath:savePath];
    // 获取资源
    AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:saveUrl options:nil];
    // 导出资源中的属性
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    // 压缩视频
    if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) {
        // 导出属性是否包含低分辨率
        // 通过资源（AVURLAsset）来定义 AVAssetExportSession，得到资源属性来重新打包资源 （AVURLAsset, 将某一些属性重新定义
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality];
        // 设置导出文件的存放路径
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
        NSDate    *date = [[NSDate alloc] init];
        NSString *outPutPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"output-%@.mp4",[formatter stringFromDate:date]]];
        exportSession.outputURL = [NSURL fileURLWithPath:outPutPath];
        
        // 是否对网络进行优化
        exportSession.shouldOptimizeForNetworkUse = true;
        
        // 转换成MP4格式
        exportSession.outputFileType = AVFileTypeMPEG4;
        [self playMovie];
        NSLog(@"%@",outPutPath);
        // 开始导出
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            // 如果导出状态为完成
            if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSLog(@"%@",outPutPath);
                    
                });
            }
        }];

    }
    
    
}


- (void)playMovie{
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = self.frame;
    [self.layer addSublayer:playerLayer];
}

- (AVPlayer *)player{
    if (!_player) {
        AVPlayerItem *playerItem = [self getPlayItem];
        _player = [AVPlayer playerWithPlayerItem:playerItem];
    }
    return _player;
}

- (NSString *)moviePat{
    if (!_moviePat) {
        _moviePat = [NSString string];
    }
    return _moviePat;
}


- (AVPlayerItem *)getPlayItem{
    NSURL *url = [NSURL URLWithString:self.moviePat];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    return playerItem;
}




@end
